package org.linjie.cocacola.yiluchunyue2019.service;


import com.emallos.xiama.dto.MemberDto;
import com.emallos.xiama.model.MemberModel;
import com.emallos.xiama.repository.MemberRepository;
import com.emallos.xiama.support.repository.BaseRepository;
import com.emallos.xiama.support.repository.MyCriteriaParams;
import com.emallos.xiama.support.repository.PageResult;
import com.emallos.xiama.support.repository.hibernate.CriteriaParams;
import com.emallos.xiama.support.service.SupportService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.google.common.base.Strings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.transaction.annotation.Transactional;
import java.util.Date;
import java.math.BigDecimal;
import java.util.List;


/**
 *  ,服务类
 * @author cnlinjie@foxmail.com
 */
@Service
public class MemberService  extends SupportService<MemberModel,Long> {

    private final static Logger logger = LoggerFactory.getLogger(MemberService.class);

    @Autowired
    private MemberRepository repository;

    /**
     * 分页查询
     * @param dto 分页查询参数
     * @return
     */
    public PageResult page(MemberDto dto) {
        CriteriaParams params = MyCriteriaParams.get();
        if (!Strings.isNullOrEmpty(dto.getKeyword())) {
         //   params.add(Restrictions.like("keyword", "%" + dto.getKeyword() + "%"));
        }
        return allOrPage(params, dto);
    }

    /**
     * 创建
     */
    @Transactional
    public Long save(MemberDto dto) {
        MemberModel model = new MemberModel(dto.getWxOpenId() ,dto.getWxUnionId() ,dto.getWxHeadImg() ,dto.getWxNickname() ,dto.getRealname() ,dto.getGender() ,dto.getPhone() ,dto.getAge() ,dto.getSosRealname() ,dto.getSosPhone() ,dto.getCardId() ,dto.getBirthday() ,dto.getCardType() ,dto.getClothesSize() ,dto.getDepositPayStatus() ,dto.getDonationTotal() ,dto.getBringDonationTotal() ,dto.getUnreadMessage() ,dto.getUnoperMessage() ,dto.getFromSource() ,dto.getLastMessage() ,dto.getNote() ,dto.getOrg() );
        repository.save(model);
        return model.getId();
    }

    /**
     * 全量更新
     */
    @Transactional
    public void update(MemberDto dto) {
        MemberModel model = getById(dto.getId());
        model.update(
            dto.getWxOpenId() ,dto.getWxUnionId() ,dto.getWxHeadImg() ,dto.getWxNickname() ,dto.getRealname() ,dto.getGender() ,dto.getPhone() ,dto.getAge() ,dto.getSosRealname() ,dto.getSosPhone() ,dto.getCardId() ,dto.getBirthday() ,dto.getCardType() ,dto.getClothesSize() ,dto.getDepositPayStatus() ,dto.getDonationTotal() ,dto.getBringDonationTotal() ,dto.getUnreadMessage() ,dto.getUnoperMessage() ,dto.getFromSource() ,dto.getLastMessage() ,dto.getNote() ,dto.getOrg() 
        );
        repository.save(model);
    }

    @Transactional
    public void deleteById(Long id) {
        MemberModel model = getById(id);
        model.markAsRemoved();
        repository.save(model);
    }

    public MemberModel getById(Long id) {
        return getById(id);
    }

    @Override
    protected BaseRepository<MemberModel, Long> getRepository() {
        return repository;
    }
}
